<?php
namespace services;
//require_once "../vendor/Yunpian/YunpianAutoload.php";
use think\facade\Session;
class SmsService extends BaseService{
   public $key="6089fdc70d4e10dd12adbbda89b59459";
   public $sendUrl = 'http://v.juhe.cn/sms/send';
   public function __construct(){
   }
   public function out_send($phone,$content=''){
       $smsOperator = new \SmsOperator();
       $data=array();
       $data['mobile'] = $phone;
       $data['text'] = $content;
       $result = $smsOperator->single_send($data);
       $info = array();
       if(!$result->success){
           $this->error_data['ErrorCode']=1;
           $this->error_data['ErrorMsg']='发送失败'.$result->responseData['msg'];
           $info['Status']='Error';
           $info['StatusInfo']=$result->responseData['msg'];
           $info['StatusContent']=json_encode($result->responseData);
       }else{
           $this->error_data['ErrorCode']=0;
           $this->error_data['ErrorMsg']="发送成功";
           $info['Status']='success';
       }
       $this->error_data['Data']=$info;
       return $this->error_data;
   }
   public function send($input){
       $phone = isset($input["phone"])?$input["phone"]:"";
       $type = isset($input["type"])?intval($input['type']):0;
       $tpl_id = isset($input["tpl_id"])?intval($input['tpl_id']):74249;
       if(empty($phone)){
           $this->error_data['ErrorMsg']='请输入手机号码';
           return $this->error_data;
       }
       if(!preg_match("/^1[0-9]{10}$/i",$phone)){
           $this->error_data['ErrorMsg']='手机号格式不对';
           return $this->error_data;
       }
       $now_time = time();
       $strCode=Session::get('LPS'.$phone);
       $CodeAr = explode("\t",$strCode);
       $sms_number=isset($CodeAr[0])?$CodeAr[0]:"";
       $ctime = isset($CodeAr[1])?abs($CodeAr[1]):0;
       if($now_time - $ctime <=100){
           $this->error_data['ErrorCode']=0;
           $this->error_data['ErrorMsg']='请等待上次发送时间100秒后再发送';
           return $this->error_data;
       }
       $sms_number=rand(111111,999999);
       /*$smsOperator = new \SmsOperator();
       $data['mobile'] = $phone;
       $data['text'] = '【乐闪派】您的验证码是'.$sms_number;
       $result = $smsOperator->single_send($data);
       */
       $smsConf = array(
           'key'=>$this->key,
           'mobile'=>$phone,
           'tpl_id'=>$tpl_id,
           'tpl_value'=>"#code#=".$sms_number,
       );
       $jsonstr=juhecurl($this->sendUrl,$smsConf,1);
       $result['error_code']=1;
       $result['reason']='';
       if($jsonstr){
           $result = @json_decode($jsonstr,1);
       }
       $info=array('phone'=>$smsConf['mobile'],'verify'=>$sms_number);
       if($type == 0 || $type == 1){
           $info['type']=1;
       }else{
           $info['type']=2;
       }
       if(!isset($result['error_code']) || $result['error_code'] !=0){
           $this->error_data['ErrorCode']=0;
           $this->error_data['ErrorMsg']='发送失败';
           $info['status']=3;
           $info['error_reason']=isset($result['reason'])?$result['reason']:'';
           //$info['StatusContent']=json_encode($result->responseData);
       }else{
           Session::set('LPS'.$phone,$sms_number."\t".$now_time);
           $this->error_data['ErrorCode']=1;
           $this->error_data['ErrorMsg']="发送成功";
           $info['status']=1;
           $info['period']=$now_time+600;
       }
       $info['create_time']=$now_time;
       $this->error_data['Data']=$info;
       return $this->error_data;
   }
}
